<?php
defined( '_JEXEC' ) or die;

class PricerUpdaterTableVendorSynonym extends JTable {

	public function __construct(&$db) {
		parent::__construct('#__pricer_vendor_synonym', 'id', $db);
	}

	public function publish($pks = null, $state = 1, $userId = 0) {
		// Initialise variables.
		$k = $this->_tbl_key;

		// Sanitize input.
		JArrayHelper::toInteger($pks);
		$userId = (int) $userId;
		$state  = (int) $state;

		// If there are no primary keys set check to see if the instance key is set.
		if (empty($pks)) {
			if ($this->$k) {
				$pks = array($this->$k);
			}
			// Nothing to set publishing state on, return false.
			else {
				$this->setError(JText::_('JLIB_DATABASE_ERROR_NO_ROWS_SELECTED'));
				return false;
			}
		}

		// Get an instance of the table
		$table = JTable::getInstance('VendorSynonym', 'PricerUpdaterTable');

		// For all keys
		foreach ($pks as $pk) {
			// Load the banner
			if(!$table->load($pk)) {
				$this->setError($table->getError());
			}

			// Verify checkout
			if($table->checked_out==0 || $table->checked_out==$userId) {
				// Change the state
				$table->state = $state;
				$table->checked_out=0;
				$table->checked_out_time=$this->_db->getNullDate();

				// Check the row
				$table->check();

				// Store the row
				if (!$table->store()) {
					$this->setError($table->getError());
				}
			}
		}
		return count($this->getErrors())==0;
	}
}
